//////////////////////////////////////////////////////////////////////////
#include "../Texture/Texture.h"
#include "TextureManager.h"
#include <cassert>
//////////////////////////////////////////////////////////////////////////
std::map<std::wstring, Texture*> TextureManager::m_textures;
std::map<std::wstring, int>		TextureManager::m_texturesCount;
//////////////////////////////////////////////////////////////////////////
void TextureManager::freeTexture( Texture* texture )
{
	std::wstring path = texture->texturePath();
	assert(m_texturesCount.find(path) != m_texturesCount.end());
	assert(m_textures[path] > 0);

	if(m_textures.find(path) != m_textures.end() && m_textures[path] > 0)
	{
		--m_texturesCount[path];
		if(m_texturesCount[path] == 0)
		{
			m_textures.erase(path);
			m_texturesCount.erase(path);
			delete texture;	
		}
	}
}
//////////////////////////////////////////////////////////////////////////
Texture* TextureManager::loadTexture( const wchar_t* path )
{
	Texture* tex = NULL;
	std::wstring strPath = path;
	if(m_texturesCount.find(strPath) != m_texturesCount.end())
	{
		tex = m_textures[strPath];
	}
	else
	{
		tex = new Texture(path);
		m_textures[strPath] = tex;
		m_texturesCount[strPath] = 0;
	}

	++m_texturesCount[path];
	return tex;
}
//////////////////////////////////////////////////////////////////////////
